package cn.jbolt.admin.globalconfig;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.cache.JBoltGlobalConfigCache;
import cn.jbolt.core.model.GlobalConfig;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.para.JBoltNoUrlPara;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltGlobalConfigService;
import cn.jbolt.core.service.JBoltGlobalConfigTypeService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;

@UnCheckIfSystemAdmin
@CheckPermission(PermissionKey.GLOBALCONFIG)
@Path("/api/admin/globalConfig")
public class GlobalConfigAdminController extends JBoltApiBaseController {
    @Inject
    private JBoltGlobalConfigTypeService globalConfigTypeService;
    @Inject
    private JBoltGlobalConfigService service;

    /**
     * 单个详情信息读取
     */
    @JBoltHttpGet
    public void index(){
        renderJBoltApiRet(findModelById());
    }

    /**
     * 单个详情信息读取 通过KEY url直接参数
     */
    @JBoltHttpGet
    public void getByKey(){
        renderJBoltApiSuccessWithData(JBoltGlobalConfigCache.me.get(get(0)));
    }

    /**
     * 单个详情信息读取 通过KEY url直接参数
     */
    @JBoltHttpGet
    @Before(JBoltNoUrlPara.class)
    public void getByKeys(){
        String[] keys = getParaToArray("keys");
        if(notOk(keys)){
            renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
            return;
        }
        renderJBoltApiSuccessWithData(JBoltGlobalConfigCache.me.getConfigsKv(keys));
    }

    /**
     * admin数据源
     * @param typeId
     * @param builtIn
     * @param keywords
     */
    public void datas(Long typeId, Boolean builtIn,String keywords) {
        renderJBoltApiSuccessWithData(service.getAdminList(typeId,builtIn,keywords));
    }

    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")GlobalConfig globalConfig){
        renderJBoltApiRet(service.update(globalConfig));
    }

    /**
     * 更新自定义参数
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void updateCustomConfig(@Para("")GlobalConfig globalConfig){
        renderJBoltApiRet(service.updateCustomConfig(globalConfig));
    }
    /**
     * 保存自定义参数
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void saveCustomConfig(@Para("")GlobalConfig globalConfig){
        renderJBoltApiRet(service.saveCustomConfig(globalConfig));
    }

    /**
     * 切换Boolean类型的值
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleBooleanValue() {
        renderJBoltApiRet(service.toggleBooleanValue(getId()));
    }

    /**
     * 删除自定义参数
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void deleteCustomConfig(){
        renderJBoltApiRet(service.deleteCustomConfig(getId()));
    }

    /**
     * 清空缓存
     */
    @JBoltHttpGet
    public void clearCache() {
        service.clearCache();
        globalConfigTypeService.clearCache();
        renderJBoltApiSuccess();
    }

   /* *//**
     * 重置数据
     *//*
    @Before(Tx.class)
    @JBoltHttpGet
    public void clearReset() {
        renderJBoltApiRet(service.clearReset());
    }*/

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }
}
